/*

	This file defines constructs regression samples at different levels of aggregation

*/

cd ${work}4_reg_variables


*** FIRM-YEAR SAMPLE
*STARTING POINT IS FIRM-YEARS IN PRODCOM
use jurnr year using input\vars_ipt, clear
duplicates drop

*MERGE ON FIRM AND FIRE (MATCH TO FIRE ALREADY REQUIRED IN VARS SAMPLE)
merge 1:1 jurnr year using input\firm_it, assert(2 3) keep(3) keepusing(nace1 employees) nogen

*REQUIRE MANUFACTURING AND 10 EMPLOYEES YEAR-BY-YEAR
keep if nace1=="C" & employees>=9.5

*REQUIRE MATCH TO LAGGED FIRM IN ORDER TO OBTAIN LAGGED TOTAL SALES
preserve
	use input\firm_it, clear
	keep jurnr year
	replace year=year+1
	save temp\temp, replace
restore
merge 1:1 jurnr year using temp\temp, keep(3) keepusing(jurnr) nogen
erase temp\temp.dta

*SAVE
keep jurnr year
compress
save output\sample_it, replace


*** FIRM-PRODUCT-YEAR SAMPLE
use jurnr vnr mgd_tekst year using input\vars_ipt, clear

merge m:1 jurnr year using output\sample_it, assert(1 3) keep(3) keepusing(jurnr) nogen

rename mgd_tekst unit
compress
save output\sample_ipt, replace


*** DESTINATION SAMPLE
use land using input\promotion, clear
duplicates drop
compress
save output\sample_d, replace


*** FIRM-DESTINATION-YEAR SAMPLE
*FIRM-YEAR SAMPLE BALANCED IN DESTINATIONS
use output\sample_d, clear
levelsof land, local(levels)

use output\sample_it, clear

foreach l of local levels {
	gen i`l'=.
}
reshape long i, i(jurnr year) j(land) string
drop i

compress
save output\sample_idt, replace